//
//  StartScene.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-4-15.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "StartScene.h"
#import "BattleScene.h"
#import "SimpleAudioEngine.h"

@implementation StartScene

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	StartScene *layer = [StartScene node];
	
	// add layer as a child to scene
	[scene addChild: layer];
     
	// return the scene
	return scene;
}

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) 
    {
        //缓冲图片文件
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UIMain_480_320.plist"];
        
        // ask director the the window size
        size = [[CCDirector sharedDirector] winSize];
        
        //初始化背景
        [self initBackGround];
        
        //初始化头像
        [self initHead];
        
        //初始化菜单
        [self initMenu];
        
        //初始化信息栏
        [self initInfo];
        
        //播放音乐
        [self schedule:@selector(checkBackgroundMusic:)]; 
    }
	return self;
}

/**
	UI背景
 */
-(void) initBackGround
{
    CCSprite* backGround = [CCSprite spriteWithSpriteFrameName:@"backGround.jpg"]; 
    backGround.position = ccp(size.width / 2 , size.height / 2);
    [self addChild:backGround];
}

/**
	UI头像
 */
-(void) initHead
{
    CCSprite* head = [CCSprite spriteWithSpriteFrameName:@"head.png"];
    CCSprite* headBG = [CCSprite spriteWithSpriteFrameName:@"headBG.png"];
    CCSprite* titleBG = [CCSprite spriteWithSpriteFrameName:@"titleBG.png"];
    
    titleBG.scaleX = size.width;
    titleBG.position = ccp(titleBG.contentSize.width / 2, size.height - titleBG.contentSize.height / 2);
    [self addChild:titleBG];
    
    headBG.position = ccp(headBG.contentSize.width / 2, size.height - headBG.contentSize.height / 2);
    [self addChild:headBG];
    
    head.position = ccp(head.contentSize.width / 2 + 11, size.height - head.contentSize.height / 2 - 12);
    [self addChild:head];
}


/**
	初始化菜单项
 */
-(void) initMenu
{
    CCSprite *baseNormal = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];
	CCSprite *baseClick = [CCSprite spriteWithSpriteFrameName:@"buttonClick.png"]; 
    CCSprite *fleetNormal = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];
	CCSprite *fleetClick = [CCSprite spriteWithSpriteFrameName:@"buttonClick.png"]; 
    CCSprite *plotNormal = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];
	CCSprite *plotClick = [CCSprite spriteWithSpriteFrameName:@"buttonClick.png"]; 
    CCSprite *captainNormal = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];
	CCSprite *captainClick = [CCSprite spriteWithSpriteFrameName:@"buttonClick.png"]; 
    CCSprite *hangarNormal = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];
	CCSprite *hangarClick = [CCSprite spriteWithSpriteFrameName:@"buttonClick.png"]; 
    CCSprite *universeNormal = [CCSprite spriteWithSpriteFrameName:@"buttonNormal.png"];
	CCSprite *universeClick = [CCSprite spriteWithSpriteFrameName:@"buttonClick.png"]; 

    // game menu
	CCMenuItemSprite* base;     //基地
    CCMenuItemSprite* fleet;    //舰队
    CCMenuItemSprite* plot;     //剧情
    
    CCMenuItemSprite* captain;   //舰长
    CCMenuItemSprite* hangar;    //机库
    CCMenuItemSprite* universe;  //宇宙
    
    base = [CCMenuItemSprite itemFromNormalSprite:baseNormal selectedSprite:baseClick target:self selector:@selector(onBaseButton:)];
    base.position = ccp(baseNormal.contentSize.width / 2, size.height - baseNormal.contentSize.height / 2 - 72);

    
    fleet = [CCMenuItemSprite itemFromNormalSprite:fleetNormal selectedSprite:fleetClick target:self selector:@selector(onFleetButton:)];
    fleet.position = ccp(fleetNormal.contentSize.width / 2 - 
                         44, size.height - fleetNormal.contentSize.height / 2 - 147);
    
    plot = [CCMenuItemSprite itemFromNormalSprite:plotNormal selectedSprite:plotClick target:self selector:@selector(onPlotButton:)];
    plot.position = ccp(plotNormal.contentSize.width / 2, size.height - plotNormal.contentSize.height / 2 - 220);
    
    captain = [CCMenuItemSprite itemFromNormalSprite:captainNormal selectedSprite:captainClick target:self selector:@selector(onCaptainButton:)];
    captain.position = ccp(size.width - captainNormal.contentSize.width / 2, size.height - captainNormal.contentSize.height / 2 - 72);
    captain.scaleX = -1;
    
    hangar = [CCMenuItemSprite itemFromNormalSprite:hangarNormal selectedSprite:hangarClick target:self selector:@selector(onHangarButton:)];
    hangar.position = ccp(size.width - hangarNormal.contentSize.width / 2 + 44, size.height - hangarNormal.contentSize.height / 2 - 147);
    hangar.scaleX = -1;
    
    universe = [CCMenuItemSprite itemFromNormalSprite:universeNormal selectedSprite:universeClick target:self selector:@selector(onUniverseButton:)];
    universe.position = ccp(size.width - universeNormal.contentSize.width / 2, size.height - universeNormal.contentSize.height / 2 - 220);
    universe.scaleX = -1;
    
    menu = [CCMenu menuWithItems:base, fleet, plot, captain, hangar, universe, nil];
    menu.position = CGPointZero;
    [self addChild:menu]; 
    
    CCSprite *baseButtonName= [CCSprite spriteWithSpriteFrameName:@"base.png"];
    CCSprite *fleetButtonName = [CCSprite spriteWithSpriteFrameName:@"fleet.png"];
    CCSprite *plotButtonName = [CCSprite spriteWithSpriteFrameName:@"plot.png"];
    CCSprite *captainButtonName = [CCSprite spriteWithSpriteFrameName:@"captain.png"];
    CCSprite *hangarButtonName = [CCSprite spriteWithSpriteFrameName:@"hangar.png"];
    CCSprite *universeButtonName = [CCSprite spriteWithSpriteFrameName:@"universe.png"];
    
    baseButtonName.position = ccp(baseButtonName.contentSize.width / 2 + 77, size.height - baseButtonName.contentSize.height / 2 - 116);
    [self addChild:baseButtonName];
    
    fleetButtonName.position = ccp(fleetButtonName.contentSize.width / 2 + 33, size.height - fleetButtonName.contentSize.height / 2 - 191);
    [self addChild:fleetButtonName];
    
    plotButtonName.position = ccp(plotButtonName.contentSize.width / 2 + 77, size.height - plotButtonName.contentSize.height / 2 - 264);
    [self addChild:plotButtonName];
    
    captainButtonName.position = ccp(size.width - captainButtonName.contentSize.width / 2 - 77, size.height - captainButtonName.contentSize.height / 2 - 116);
    [self addChild:captainButtonName];
    
    hangarButtonName.position = ccp(size.width -hangarButtonName.contentSize.width / 2 - 33, size.height - hangarButtonName.contentSize.height / 2 - 191);
    [self addChild:hangarButtonName];
    
    universeButtonName.position = ccp(size.width -universeButtonName.contentSize.width / 2 - 77, size.height - universeButtonName.contentSize.height / 2 - 264);
    [self addChild:universeButtonName];

}

/**
	初始化信息栏
 */
-(void) initInfo
{
    //右边暂时无用得装饰品
    CCSprite *rightOrnament = [CCSprite spriteWithSpriteFrameName:@"rightOrnament.png"];
    rightOrnament.position = ccp(rightOrnament.contentSize.width / 2 + 412, size.height - rightOrnament.contentSize.height / 2 - 10);
    [self addChild:rightOrnament];
    
    //顶部装饰品
    CCSprite *topOrnament = [CCSprite spriteWithSpriteFrameName:@"topOrnament.png"];
    topOrnament.position = ccp(topOrnament.contentSize.width / 2 + 300, size.height - topOrnament.contentSize.height / 2);
    [self addChild:topOrnament];
    
    //名字背景
    CCSprite *nameBGLeft = [CCSprite spriteWithSpriteFrameName:@"nameBG.png"];//名字左边背景
    nameBGLeft.position = ccp(nameBGLeft.contentSize.width / 2 + 80, size.height - nameBGLeft.contentSize.height / 2);
    [self addChild:nameBGLeft];
    
    CCSprite *nameBGRight = [CCSprite spriteWithSpriteFrameName:@"nameBG.png"];//名字右边背景
    nameBGRight.scaleX = -1;
    nameBGRight.position = ccp(nameBGRight.contentSize.width / 2 + 168, size.height - nameBGRight.contentSize.height / 2);
    [self addChild:nameBGRight];
    
    //名字 
    CCLabelTTF *name = [CCLabelTTF labelWithString:@"李思嘉"  fontName:@"Marker Felt"  fontSize:12];
    name.anchorPoint = ccp(0.5, 1);
    name.position = ccp(168, size.height - 3);
    [self addChild:name];
    
    //军衔背景及字
    CCSprite *militaryRankBG = [CCSprite spriteWithSpriteFrameName:@"gridBG.png"];//数值框背景
    militaryRankBG.position = ccp(militaryRankBG.contentSize.width / 2 + 82, size.height - militaryRankBG.contentSize.height / 2 - 20);
    [self addChild:militaryRankBG];
    CCSprite *militaryRank = [CCSprite spriteWithSpriteFrameName:@"militaryRank.png"];//字样背景
    militaryRank.position = ccp(militaryRank.contentSize.width / 2 + 97, size.height - militaryRank.contentSize.height / 2 - 22);
    [self addChild:militaryRank];
    CCLabelTTF *militaryRankName = [CCLabelTTF labelWithString:@"一等兵"  fontName:@"Marker Felt"  fontSize:10];//军衔
    militaryRankName.color =  ccc3(0, 255, 255);//蓝色字
    militaryRankName.anchorPoint = ccp(0.5, 1);
    militaryRankName.position = ccp(187, size.height - 23);
    [self addChild:militaryRankName];
    
    //声望背景及字
    CCSprite *prestigeBG = [CCSprite spriteWithSpriteFrameName:@"gridBG.png"];//数值框背景
    prestigeBG.position = ccp(prestigeBG.contentSize.width / 2 + 82, size.height - prestigeBG.contentSize.height / 2 - 38);
    [self addChild:prestigeBG];
    CCSprite *prestige = [CCSprite spriteWithSpriteFrameName:@"prestige.png"];//字样背景
    prestige.position = ccp(prestige.contentSize.width / 2 + 97, size.height - prestige.contentSize.height / 2 - 40);
    [self addChild:prestige];
    CCLabelTTF *prestigeName = [CCLabelTTF labelWithString:@"300"  fontName:@"Marker Felt"  fontSize:10];//声望
    prestigeName.color =  ccc3(0, 255, 255);//蓝色字
    prestigeName.anchorPoint = ccp(0.5, 1);
    prestigeName.position = ccp(187, size.height - 41);
    [self addChild:prestigeName];
    
    //战功背景及字
    CCSprite *meritoriousBG = [CCSprite spriteWithSpriteFrameName:@"gridBG.png"];//数值框背景
    meritoriousBG.position = ccp(meritoriousBG.contentSize.width / 2 + 82, size.height - meritoriousBG.contentSize.height / 2 - 57);
    [self addChild:meritoriousBG];
    CCSprite *meritorious = [CCSprite spriteWithSpriteFrameName:@"meritorious.png"];//字样背景
    meritorious.position = ccp(meritorious.contentSize.width / 2 + 97, size.height - meritorious.contentSize.height / 2 - 59);
//    [self addChild:meritorious];
//    CCSprite *meritoriousProgress = [CCSprite spriteWithSpriteFrameName:@"战功进度条拉伸.png"];//战功进度
//    meritoriousProgress.scaleX = 107;
//    meritoriousProgress.position = ccp(meritoriousProgress.contentSize.width / 2 + 136, size.height - meritoriousProgress.contentSize.height / 2 - 60);
//    [self addChild:meritoriousProgress];
    
    //能量背景及字
    CCSprite *energyBG = [CCSprite spriteWithSpriteFrameName:@"gridBG.png"];//数值框背景
    energyBG.position = ccp(energyBG.contentSize.width / 2 + 247, size.height - energyBG.contentSize.height / 2 - 20);
    [self addChild:energyBG];
    CCSprite *energy = [CCSprite spriteWithSpriteFrameName:@"energy.png"];//字样背景
    energy.position = ccp(energy.contentSize.width / 2 + 263, size.height - energy.contentSize.height / 2 - 22);
    [self addChild:energy];
    CCSprite *energyGap = [CCSprite spriteWithSpriteFrameName:@"gridGapBG.png"];//中间得间隔
    energyGap.position = ccp(energyGap.contentSize.width / 2 + 350, size.height - energyGap.contentSize.height / 2 - 22);
    [self addChild:energyGap];
    CCLabelTTF *energyName = [CCLabelTTF labelWithString:@"25/25"  fontName:@"Marker Felt"  fontSize:10];//能量
    energyName.color =  ccc3(0, 255, 0);//绿色字
    energyName.anchorPoint = ccp(0.5, 1);
    energyName.position = ccp(327, size.height - 23);
    [self addChild:energyName];
    CCLabelTTF *timeName = [CCLabelTTF labelWithString:@"00:00:00"  fontName:@"Marker Felt"  fontSize:10];//时间
    timeName.color =  ccc3(0, 255, 255);//蓝色字
    timeName.anchorPoint = ccp(0.5, 1);
    timeName.position = ccp(382, size.height - 23);
    [self addChild:timeName];
    
    
    //军需背景及字
    CCSprite *militarySuppliesBG = [CCSprite spriteWithSpriteFrameName:@"gridBG.png"];//数值框背景
    militarySuppliesBG.position = ccp(militarySuppliesBG.contentSize.width / 2 + 247, size.height - militarySuppliesBG.contentSize.height / 2 - 38);
    [self addChild:militarySuppliesBG];
    CCSprite *militarySupplies = [CCSprite spriteWithSpriteFrameName:@"militarySupplies.png"];//字样背景
    militarySupplies.position = ccp(militarySupplies.contentSize.width / 2 + 263, size.height - militarySupplies.contentSize.height / 2 - 40);
    [self addChild:militarySupplies];
    CCLabelTTF *militarySuppliesName = [CCLabelTTF labelWithString:@"14000"  fontName:@"Marker Felt"  fontSize:10];//军需
    militarySuppliesName.color =  ccc3(0, 255, 255);//蓝色字
    militarySuppliesName.anchorPoint = ccp(0.5, 1);
    militarySuppliesName.position = ccp(361, size.height - 41);
    [self addChild:militarySuppliesName];
    
    //金币背景及字
    CCSprite *goldCoinBG = [CCSprite spriteWithSpriteFrameName:@"gridBG.png"];//数值框背景
    goldCoinBG.position = ccp(goldCoinBG.contentSize.width / 2 + 247, size.height - goldCoinBG.contentSize.height / 2 - 57);
    [self addChild:goldCoinBG];
    CCSprite *goldCoin = [CCSprite spriteWithSpriteFrameName:@"goldCoin.png"];//字样背景
    goldCoin.position = ccp(goldCoin.contentSize.width / 2 + 263, size.height - goldCoin.contentSize.height / 2 - 59);
    [self addChild:goldCoin];
    CCLabelTTF *goldCoinName = [CCLabelTTF labelWithString:@"3200000"  fontName:@"Marker Felt"  fontSize:10];//金币
    goldCoinName.color =  ccc3(0, 255, 255);//蓝色字
    goldCoinName.anchorPoint = ccp(0.5, 1);
    goldCoinName.position = ccp(361, size.height - 61);
    [self addChild:goldCoinName];

}


/**
	处理基地按钮得点击
	@param sender  事件发出者
 */
- (void) onBaseButton:(id)sender
{
	CCTransitionFade* trans = [CCTransitionFade transitionWithDuration:1.0 scene:[BattleScene scene]];
	[[CCDirector sharedDirector] replaceScene: trans];
}


/**
	处理舰队按钮得点击
	@param sender 事件发出者
 */
- (void) onFleetButton:(id)sender 
{
    //TODO
}

/**
	处理剧情按钮的点击
	@param sender 事件发出者
 */
- (void) onPlotButton:(id)sender
{
    //TODO
}


/**
	处理舰长按钮的点击
	@param sender 事件发出者
 */
- (void) onCaptainButton:(id)sender
{
    //TODO
}

/**
	处理机库按钮的点击
	@param sender 事件发出者
 */
- (void) onHangarButton:(id)sender
{
    //TODO
}

/**
	处理宇宙按钮的点击
	@param sender 事件发出者
 */
- (void) onUniverseButton:(id)sender
{
    //TODO
}

-(void) checkBackgroundMusic:(ccTime)aTime
{
    [self unschedule:@selector(checkBackgroundMusic:)]; 
    
    [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"menu.mp3"];  
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
